/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kindergarden.at.google;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.*;
import javax.swing.JTable;

/**
 *
 * @author rogu0903
 */
public class MySqlListener implements Runnable{
// private  Connection con ;
//   private  Statement st ;
//   private  ResultSet rs;
//   private  Statement s;
   private  ResultSet rs;
   private  Statement s;
   private long myLastUpdate;

    public void run() {
     try {
         	s = DatabaseConnection.connection.createStatement ();
    		   		s.executeQuery("SELECT* FROM version");

    		   		rs = s.getResultSet ();
    		   		rs.next ();
                                
    		   		myLastUpdate=rs.getLong("lastupdate");
        } catch (SQLException ex) {
            Logger.getLogger(MySqlListener.class.getName()).log(Level.SEVERE, null, ex);
        }
       while(true){
    	   
       try {
    	   
    		   		s = DatabaseConnection.connection.createStatement ();
    		   		s.executeQuery("SELECT* FROM version");

    		   		rs = s.getResultSet ();
    		   		rs.next ();
    		   		System.out.println(rs.getLong("lastupdate"));
                                
                                if (rs.getLong("lastupdate")>myLastUpdate){
                                    myLastUpdate=rs.getLong("lastupdate");
                                    rs.close();
                                    s.close();
                                    System.out.println("Jävlar nu uppdateras listan");
                                    
                                    
                               
                                             GUI.myTable=GUI.a.getResultSetTableModel("SELECT name, id, attendence FROM child");
                                             GUI.myTable.fireTableDataChanged();
                                             
                                             GUI.table.setModel(GUI.myTable);
                                            // GUI.table.fireTableStructureChanged();
                                        //    GUI.table.re;
                                        }
                                
            } catch (SQLException ex) {
            Logger.getLogger(MySqlListener.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {

                         Thread.sleep(1000);

                    } catch (InterruptedException ex) {
                   
                }
    }
    }

}
